package io.renren.modules.generator.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.generator.entity.CommentEntity;
import io.renren.modules.generator.service.CommentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 商品评论表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-04-05 12:40:56
 */
@RestController
@RequestMapping("generator/comment")
@Api("商品评论")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @ApiOperation("商品评论列表")
    @GetMapping("/list")
    @RequiresPermissions("generator:comment:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = commentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("单条商品评论")
    @GetMapping("/info/{id}")
    @RequiresPermissions("generator:comment:info")
    public R info(@PathVariable("id") String id){
        List<CommentEntity> list = commentService.list(new QueryWrapper<CommentEntity>().eq("commodity_id", id));

        for (CommentEntity entity : list) {
            Long userId = entity.getUserId();
            SysUserEntity user = sysUserService.getById(userId);

            entity.setUsername(user.getUsername());
        }

        return R.ok().put("comment", list);
    }

    /**
     * 信息
     */
    @ApiOperation("单条商品评论")
    @GetMapping("/common/info/{id}")
    @RequiresPermissions("generator:comment:info")
    public R commonInfo(@PathVariable("id") String id){
        CommentEntity comment = commentService.getById(id);

        return R.ok().put("comment", comment);
    }

    /**
     * 保存
     */
    @ApiOperation("保存商品评论")
    @PostMapping("/save")
    @RequiresPermissions("generator:comment:save")
    public R save(@RequestBody CommentEntity comment){
		commentService.save(comment);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改商品评论")
    @PostMapping("/update")
    @RequiresPermissions("generator:comment:update")
    public R update(@RequestBody CommentEntity comment){
		commentService.updateById(comment);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除商品评论")
    @PostMapping("/delete")
    @RequiresPermissions("generator:comment:delete")
    public R delete(@RequestBody Long[] ids){
		commentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
